﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;

namespace AutomationFoundation.CodeToolKit.VisualStudio.Linq
{
    internal static class QueryableProjectItems
    {
        public static IEnumerable<TElement> GetProjectItems<TElement>(ProjectItems source)
        {
            ProjectItemIterator iterator = new ProjectItemIterator(source);
            return iterator.OfType<TElement>();
        }

        /// <summary>
        /// Gets element filtered by where condition.
        /// </summary>
        /// <typeparam name="TElement">The type of the element.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="predicate">The predicate.</param>
        /// <returns></returns>
        public static TElement GetElementWhere<TElement>(ProjectItems source, Func<TElement, bool> predicate)
        {
            return GetProjectItems<TElement>(source).SingleOrDefault(predicate);
                
        }

        /// <summary>
        /// Gets element filtered by where condition.
        /// </summary>
        /// <typeparam name="TElement">The type of the element.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="predicate">The predicate.</param>
        /// <returns></returns>
        public static IEnumerable<TElement> GetElementsWhere<TElement>(ProjectItems source, Func<TElement, bool> predicate)
        {
            return GetProjectItems<TElement>(source).Where(predicate).Select(P => P);

        }
    }
}
